package com.example.hissys.controller;

import com.example.hissys.entity.Department;
import com.example.hissys.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

/**
 * 科室控制器
 */
@RestController
@RequestMapping("/api/departments")
@CrossOrigin(origins = "*")
@Slf4j
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 根据医院ID获取科室列表
     * @param hospitalId 医院ID
     * @return 科室列表
     */
    @GetMapping("/hospital/{hospitalId}")
    public ResponseEntity<?> getDepartmentsByHospitalId(@PathVariable Integer hospitalId) {
        try {
            List<Department> departments = departmentService.getDepartmentsByHospitalId(hospitalId);
            if (departments.isEmpty()) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(departments);
        } catch (Exception e) {
            log.error("Error fetching departments for hospital ID: {}", hospitalId, e);
            return ResponseEntity.badRequest().body("获取科室列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取部门详情
     * @param id 部门ID
     * @return 部门详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getDepartmentById(@PathVariable Integer id) {
        try {
            Department department = departmentService.getDepartmentById(id);
            if (department == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(department);
        } catch (Exception e) {
            log.error("Error fetching department with ID: {}", id, e);
            return ResponseEntity.badRequest().body("获取部门详情失败：" + e.getMessage());
        }
    }
} 